<template>
  <div class="app-container">
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="keyWord"
            placeholder="请输入想要查询的信息"
            @keyup.enter.native.prevent="searchCourseByName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchCourseByName"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="dialogAddVisible = true"
            icon="el-icon-plus"
            >添加</el-button
          >
          <el-dialog title="新增一个课程信息" :visible.sync="dialogAddVisible">
            <el-form @submit.native.prevent :model="courseInfo">
              <el-form-item label="课程名称">
                <el-input v-model="courseInfo.cName"></el-input>
              </el-form-item>
              <el-form-item label="授课老师">
                <el-select v-model="courseInfo.tId" placeholder="请选择">
                  <el-option
                    v-for="course in courses"
                    :key="course.cId"
                    :label="course.teacherName"
                    :value="course.tId"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="学分">
                <el-input
                  v-model="courseInfo.credit"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="上课地点">
                <el-input
                  v-model="courseInfo.address"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="课程类型">
                <el-select v-model="courseInfo.cType" placeholder="请选择">
                  <el-option label="选修课" value="选修课"></el-option>
                  <el-option label="必修课" value="必修课"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="开始时间">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="courseInfo.startDate"
                  style="width: 100%"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="结束时间">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="courseInfo.endDate"
                  style="width: 100%"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="上课时间">
                <el-time-picker
                  placeholder="选择时间"
                  v-model="courseInfo.classTime"
                  style="width: 100%"
                ></el-time-picker>
              </el-form-item>
              <el-form-item label="课时">
                <el-input
                  v-model="courseInfo.courseTime"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="剩余人数">
                <el-input
                  v-model="courseInfo.count"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogAddVisible = false">取 消</el-button>
              <el-button type="primary" @click="addCourseSubmit"
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </el-form-item>
      </el-form>

      <!-- CourseTable-->
      <el-table
        v-loading="listLoading"
        :data="courses"
        element-loading-text="Loading"
        stripe
        height="550"
        border
        fits
        highlight-current-row
      >
        <el-table-column align="center" label="课程编号" prop="cId" />
        <el-table-column label="课程名称">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.cName"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.cName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="授课教师" align="center">
          <template slot-scope="{ row }">
            <el-select
              v-if="row.isEdit"
              v-model="row.tId"
              :placeholder="row.teacherName"
              size="small"
            >
              <el-option
                v-for="course in courses"
                :key="course.cid"
                :label="course.teacherName"
                :value="course.tId"
              ></el-option>
            </el-select>
            <span v-else>{{ row.teacherName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="学分" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.credit"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.credit }}</span>
          </template>
        </el-table-column>

        <el-table-column label="上课地点" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.address"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.address }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="课程类型">
          <template slot-scope="{ row }">
            <el-select
              v-if="row.isEdit"
              v-model="row.cType"
              :placeholder="row.cType"
              size="small"
            >
              <el-option label="选修课" value="选修课"></el-option>
              <el-option label="必修课" value="必修课"></el-option>
            </el-select>
            <el-tag v-else disable-transitions :type="isCourseType(row.cType)">
              {{ row.cType }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="开始时间" align="center">
          <template slot-scope="{ row }">
            <el-date-picker
              v-if="row.isEdit"
              type="date"
              placeholder="选择日期"
              v-model="row.startDate"
              style="width: 100%"
              size="small"
            >
            </el-date-picker>
            <span v-else>{{ row.startDate | dateFormater }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center">
          <template slot-scope="{ row }">
            <el-date-picker
              v-if="row.isEdit"
              type="date"
              placeholder="选择日期"
              v-model="row.endDate"
              style="width: 100%"
              size="small"
            >
            </el-date-picker>
            <span v-else>{{ row.endDate | dateFormater }}</span>
          </template>
        </el-table-column>
        <el-table-column label="上课时间" align="center">
          <template slot-scope="{ row }">
            <el-time-picker
              v-if="row.isEdit"
              type="time"
              placeholder="选择时间"
              v-model="row.classTime"
              style="width: 100%"
              size="small"
            ></el-time-picker>
            <span v-else>{{ row.classTime | timeFormater }}</span>
          </template>
        </el-table-column>
        <el-table-column label="课时" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.courseTime"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.courseTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="剩余人数" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.count"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.count }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              circle
              style="margin-left: 0"
              v-show="!row.isEdit"
              @click="row.isEdit = !row.isEdit"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              style="margin-left: 0"
              v-show="row.isEdit"
              size="small"
              @click="editOneCourse(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              @click="deleteOneCourse(row.cId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-pagination layout="prev, pager, next" :total="1000"> </el-pagination>
  </div>
</template>

<script>
import {
  getCourseList,
  addCourse,
  updateCourse,
  deleteCourse,
  search,
} from "@/api/course";
import moment from "moment";
import { nanoid } from "nanoid";

export default {
  data() {
    return {
      courses: null,
      listLoading: true,
      keyWord: "",
      courseInfo: {
        cId: nanoid(),
        cName: "",
        cType: "",
        tId: "",
        credit: "",
        address: "",
        startDate: "",
        endDate: "",
        classTime: "",
        courseTime: "",
        count: "",
      },
      dialogAddVisible: false,
    };
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getCourseList().then((response) => {
        this.courses = response.data.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
        this.listLoading = false;
      });
    },
    isCourseType(type) {
      return type == "选修课" ? "success" : "danger";
    },
    addCourseSubmit() {
      this.dialogAddVisible = false;
      addCourse(this.courseInfo).then((resp) => {
        if (resp.status == 200) {
          this.$message({
            message: resp.msg,
            type: "success",
          });
          this.courses.push(this.courseInfo);
        }
      });
    },
    editOneCourse(course) {
      course.isEdit = false;
      updateCourse(course).then((resp) => {
        if (resp.status == 200)
          this.$message({
            message: resp.msg,
            type: "success",
          });
        else
          this.$message({
            message: resp.msg,
            type: "error",
          });
      });
    },
    deleteOneCourse(cId) {
      if (confirm("你确定要删除这条数据吗?"))
        deleteCourse(cId).then((resp) => {
          this.$message({
            message: resp.msg,
            type: "success",
          });
          this.courses = this.courses.filter((item) => {
            return item.cId != cId;
          });
        });
    },
    searchCourseByName() {
      search(this.keyWord).then((resp) => {
        if (resp.status === 200) {
          this.courses = resp.data.map((v) => {
            this.$set(v, "isEdit", false);
            return v;
          });
        }
      });
    },
  },
  filters: {
    dateFormater(datetime) {
      return moment(datetime).format("YYYY-MM-DD");
    },

    timeFormater(time) {
      return moment(time).format("HH:mm:ss");
    },
  },

  created() {
    this.fetchData();
  },
};
</script>
<style scoped>
el-pagination {
  display: inline;
  margin: 0 auto;
}
</style>